package leetcode.code0725;

import leetcode.helper.tree.ListNode;

public class Solution extends Solution725 {

	@Override
	public ListNode[] splitListToParts(ListNode head, int k) {
		ListNode[] ans = new ListNode[k];
		int len = 0;
		ListNode p1 = head;
		while (p1 != null) {
			p1 = p1.next;
			len++;
		}
		int groupSize = len / k;
		int carry = len % k;
		p1 = head;
		for (int i = 0; i < k; i++) {
			if (p1 == null)//没了,后边没戏唱了
				return ans;
			int step = groupSize;
			if (carry > 0) {
				step++;
				carry--;
			}
			ans[i] = p1;//当前组答案
			while (step > 1) {//每组n个取n-1是小组最后一个,p1!=null,step至少为1
				p1 = p1.next;
				step--;
			}
			ListNode p2 = p1;//指向最后一个
			p1 = p1.next;//
			p2.next = null;
		}
		return ans;
	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
	}

}
